import os
import re
import jsmin

import StringIO

from google.appengine.ext import webapp
from google.appengine.api import memcache

import wsgiref.handlers

jsm = jsmin.JavascriptMinify()

memCacheExpire = 900

def getFileName(path):
  regex = re.compile('/[a-zA-Z_]+/([a-zA-Z0-9_\-/]+\.(js|css))$')
  return 'js/' + regex.search(path).groups()[0]

def getFileContent(filename):  
  ospath = os.path.join(os.path.dirname(__file__), filename)
  input = open(ospath, 'r')
  return input.read()

def minify(filename):    
  path = os.path.join(os.path.dirname(__file__), filename)        
  input = open(path, 'r')
  output = StringIO.StringIO()

  jsm.minify(input, output)

  return output.getvalue()  

class JsMinify(webapp.RequestHandler):
  def get(self):
        
    data = memcache.get(self.request.path)    

    if data is None:
      filename = getFileName(self.request.path)
      data = minify(filename)   
      memcache.add(self.request.path, data, memCacheExpire)      

    self.response.headers['Content-Type'] = 'text/javascript'
    self.response.out.write(data)


class JsMinify_(webapp.RequestHandler):
  def get(self):
    filename = getFileName(self.request.path)
    data = minify(filename)              

    self.response.headers['Content-Type'] = 'text/javascript'
    self.response.out.write(data)


class JsNormal(webapp.RequestHandler):
  def get(self):      

    data = memcache.get(self.request.path)    

    if data is None:
      filename = getFileName(self.request.path)
      data = getFileContent(filename)
      memcache.add(self.request.path, data, memCacheExpire)   

    self.response.headers['Content-Type'] = 'text/javascript'
    self.response.out.write(data)

class JsNormal_(webapp.RequestHandler):
  def get(self):      
    filename = getFileName(self.request.path)
    data = getFileContent(filename)

    self.response.headers['Content-Type'] = 'text/javascript'
    self.response.out.write(data)

apps_binding = []

# serving normal javascript file from mem cache
apps_binding.append(('/js/.*', JsNormal))

# serving normal javascript file directly from disk
apps_binding.append(('/js_/.*', JsNormal_))

# serving minified javascript file from mem cache
apps_binding.append(('/js_min/.*', JsMinify))

# serving minified javascript file directly from disk
apps_binding.append(('/js_min_/.*', JsMinify_))


application = webapp.WSGIApplication(apps_binding, debug=True)
wsgiref.handlers.CGIHandler().run(application)
